package com.tymchak.DAO.impl;

import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;

import com.tymchak.DAO.UserDAO;
import com.tymchak.entity.User;

@Stateless
public class UserDAOImpl extends GenericDAOImpl<User> implements UserDAO {

	public UserDAOImpl() {
		super(User.class);
	}

	@Override
	public User read() {
		
		User user = null;

		try {
			String login = ((HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest())
					.getUserPrincipal().toString();

			TypedQuery<User> query = em.createQuery(
					"SELECT u FROM User u WHERE u.login = :login",
					User.class);
			query.setParameter("login", login);
			user = query.getResultList().get(0);
		} catch (Exception e) {
			// logger.error(e.toString());
			// TODO
		}

		return user;
	}

}
